package edu.nanyang.controller;

import com.github.pagehelper.PageInfo;
import edu.nanyang.domain.PaperInfo;
import edu.nanyang.service.PaperInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import java.io.IOException;

@Controller
@RequestMapping("/admin/paper")
@Api(tags = "试卷操作接口")
public class PaperInfoController extends BaseController {
    @Resource
    private PaperInfoService paperInfoService;

    @GetMapping("/search")
    public String search(int page) {
        int pageSize=2;
        PageInfo<PaperInfo> pageInfo = paperInfoService.search(page, pageSize);
        request.setAttribute("page_vo", pageInfo);
        return "/admin/paper/paper_list";
    }

    @GetMapping("/delete")
    public String delete(@ApiParam(name = "id", value = "试卷id", required = true) @RequestParam int id) throws ServletException, IOException {
        int result = paperInfoService.deleteById(id);
        String tipInfo = "删除成功！";
        if (result < 0) {
            tipInfo = "删除失败！";
        }
        request.setAttribute("tip_info", tipInfo);
        //请求转发
        return "forward:search?page=1";

    }

    @GetMapping("/addpage")
    public String addPage() {
        return "admin/paper/paper_add";
    }

    @GetMapping("/updatepage")
    public String updatePage(@ApiParam(name = "id", value = "学院id", required = true) @RequestParam int id) {
        request.setAttribute("paper_info", paperInfoService.getById(id));
        return "admin/paper/paper_edit";
    }

    @PostMapping("/addsave")
    public String addSave(PaperInfo paperInfo) {
        int result = paperInfoService.add(paperInfo);
        String tipInfo = "添加成功！";
        if (result < 0) {
            tipInfo = "添加失败！";
        }
        request.setAttribute("tip_info", tipInfo);
        return "result";
    }

    @PostMapping("/updatesave")
    public String updateSave(PaperInfo paperInfo) {
        int result = paperInfoService.update(paperInfo);
        String tipInfo = "修改成功！";
        if (result < 0) {
            tipInfo = "修改失败！";
        }
        request.setAttribute("tip_info", tipInfo);
        return "result";
    }

}
